package com.hy.mydesktop.client.mvc.grid;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.extjs.gxt.ui.client.data.BaseModel;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.event.MenuEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.extjs.gxt.ui.client.widget.Component;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.hy.mydesktop.client.component.event.AppEvents;
import com.hy.mydesktop.client.component.factory.DesktopMenuItemFactory;
import com.hy.mydesktop.client.component.factory.GxtComponentFactory;
import com.hy.mydesktop.client.component.mvc.event.EventStructureMetaModel;
import com.hy.mydesktop.client.component.type.ComponentTypeEnum;
import com.hy.mydesktop.client.mvc.base.BaseController;
import com.hy.mydesktop.client.mvc.base.BaseDisplay;
import com.hy.mydesktop.client.mvc.core.event.manager.ComponentTrrigerTemplateUtil;
import com.hy.mydesktop.client.mvc.logindialog.LoginDialog;
import com.hy.mydesktop.client.mvc.panel.ContentPanelDisplay;
import com.hy.mydesktop.client.mvc.panel.ContentPanelPresenter;
import com.hy.mydesktop.client.mvc.util.ControllerUtil;
import com.hy.mydesktop.shared.persistence.domain.gxt.ItemOfAssociateToWindow;
import com.hy.mydesktop.shared.persistence.domain.gxt.constant.ComponentMetaDataConstants;
import com.hy.mydesktop.shared.util.log.MyLoggerUtil;

public class GridSetModulesController extends BaseController {
	private GridSetModulesPresenter gridSetModulesPresenter;

	
	public interface IGridSetModulesDiplay extends BaseDisplay{
		public Grid<ModelData> getGrid() ;
		public ComponentTypeEnum getGridModuleType() ;
		public GridSetModule getGridSetModule() ;
		


	}
	
	public GridSetModulesController() {
		registerEventTypes(AppEvents.DESKTOP_SHORTCUT_ONCLICK_INIT);

	}

	@Override
	public void handleEvent(AppEvent event) {
		Map<String, Object> map = (Map<String, Object>) event.getData();
		EventStructureMetaModel eventStructureMetaModel = (EventStructureMetaModel) map
				.get("eventstructuremetamodel");
		
		//0401
		if (AppEvents.LOAD_DATA_EVENT==event.getType()) {
			//通过EventStructureMetaModel识别，是否应该处理该事件
//			if (eventStructureMetaModel==null||eventStructureMetaModel.getControllerId()==null||eventStructureMetaModel.getControllerId()==this.getId()) {
			if (ControllerUtil.isHandle(eventStructureMetaModel, this)) {
				/**
				 * event中需要包含的内容
				 * 需要被加载数据的是哪个控件
				 * 这个控件是那种类型Grid还是Tree
				 * 该如何加载数据（eg：需要哪些标识）
				 */
				forwardToView(gridSetModulesPresenter, event);				
			}
		}
		
		// 0007
		if (AppEvents.DESKTOP_WINDOWAPPLICATION_INIT == event.getType()) {
			//if (eventStructureMetaModel==null||this.getId()==null||eventStructureMetaModel.getControllerId()==null||eventStructureMetaModel.getControllerId().equals(this.getId())) {
			if (ControllerUtil.isHandle(eventStructureMetaModel, this)) {
	
			// 转发到视图View
				forwardToView(gridSetModulesPresenter, event);
				/**
				 * 获取Controller对应的View的组件Component 把Component，作为事件的一部分，向下传递
				 * map.put("parent", windowView.getWindow()); 此方法错误
				 */
				Map<BaseController, Component> subMap = (Map<BaseController, Component>) map
						.get("controllerandcomponent");
				;
				subMap.put(this, gridSetModulesPresenter.getDisplay().getComponent());
				/**
				 * 把Controller和,与之对应的Component放入Map（为了方便查找Component的父元素）
				 */
				map.put("controllerandcomponent", subMap);
				/**
				 * 如何知道事件已经转发完毕
				 * 
				 * 方法1 ： 计数器
				 */

				if (this.getChildren() != null && this.getChildren().size() > 0) {
					/**
					 * 将事件转发给子节点，及子控制器 注:当前控制器，含有子节点的时候，必不可少
					 * 
					 * 采用先序遍历的方式---树
					 */
					this.forwardToChild(new AppEvent(
							AppEvents.DESKTOP_WINDOWAPPLICATION_INIT, map));
				} /*
				 * else {
				 */
				/**
				 * 如果当前控制器，有子节点，则向下转发事件， 否则渲染组件，即渲染顶级元素 如何渲染顶级元素，即Window ？
				 */
				/*
				 * forwardToView(windowView,
				 * AppEvents.DESKTOP_APPLICATION_RENDER,event.getData()); }
				 */
				/*int count = ((Integer) map.get("count") - 1);
				if (count <= 0) {
					Dispatcher.forwardEvent(
							AppEvents.DESKTOP_APPLICATION_RENDER, map);
				} else {
					map.put("count", count);
				}*/
				ControllerUtil.isRenderer(map);

			}
		}
		//0601
		 if (AppEvents.GRID_RELOAD == event.getType()) {			
			if (ControllerUtil.isHandle(eventStructureMetaModel, this)) {

				forwardToView(gridSetModulesPresenter, event);
				this.forwardToChild(event);
			}
			
		}		

		//0305
		else if (AppEvents.FORM_FIELD_DATALOAD==event.getType()) {
			//if (eventStructureMetaModel==null||this.getId()==null||eventStructureMetaModel.getControllerId()==null||eventStructureMetaModel.getControllerId().equals(this.getId())) {
			if (ControllerUtil.isHandle(eventStructureMetaModel, this)) {
			
			
					/*String dataObjectId = (String)map.get("dataObjectId");		
					BaseModel formData = (BaseModel)map.get("formData");						
					//String fieldValue = formData.get(property);

					String fieldId = textFieldView.getTextField().getData("id");
					int firstIndex = fieldId.indexOf(".");
					
					String fieldPropertId = fieldId.substring(firstIndex+1);2222222222222222
					
					textFieldView.getTextField().setValue((String)formData.get(fieldPropertId));*/
														
					//转发到视图View
				forwardToView(gridSetModulesPresenter, event);											
			}else {
				this.forwardToChild(event);

			}		
		}
		
		//0000
		//如果当前控制器，不能处理该事件，则向下转发
		else {
			this.forwardToChild(event);
		}
		/*if(this.canHandle(event)) {
			this.forwardToChild(event);
		}
		;*/
	}

	public void initialize() {
		
		GridSetModule  gridSetModule = GxtComponentFactory.createOneGxtComponent(getGxtComponentMetaNodeModel());					
		GridSetModuleDiplay display = new GridSetModuleDiplay(gridSetModule);
		gridSetModulesPresenter = new GridSetModulesPresenter(this,display);
		
		List<ItemOfAssociateToWindow> list =(List<ItemOfAssociateToWindow>)gridSetModule.getData(ComponentMetaDataConstants.CHILDREN_ITEMS);
		for (ItemOfAssociateToWindow item : list) {
			if (ComponentMetaDataConstants.MVC_ITEM_TYPE.equals(item.getItemType())) {
				AppEvent appEvent =ComponentTrrigerTemplateUtil.createAppEvent(gridSetModule, item);
				//发送数据，使窗口中的表格加载数据
				//0401
				//Dispatcher.get().dispatch(appEvent);
				gridSetModulesPresenter.handleEvent(appEvent);
			}
		}
												
		
		
	}

	protected void onError(AppEvent ae) {
		System.out.println("error: " + ae.<Object> getData());
	}
}
